import React from "react";
import { sleep } from 'antd-mobile/es/utils/sleep'
import styles from '@/assets/css/guest.module.css';

const GuestIndex = () => {
    let [page, SetPage] = React.useState(1)
    let [list, SetList] = React.useState([]);
    let [hasMore, SethasMore] = React.useState(false);

    //封装右侧滑动的按钮数据
    const rightBtn = [
        {
            key: 'delete',
            text: '删除住客',
            color: 'danger',
        }
    ]

    //生命周期
    React.useEffect(() => 
    {
        GetGuestData()
    }, [])

    //下拉刷新
    const OnRefresh = async () => 
    {
        SetPage(1)
        SethasMore(true);
        SetList([]);

        await sleep(1000);
        await GetGuestData();
    };

    //上拉加载
    const LoadMore = async () =>
    {
        await sleep(1000)
        GetGuestData()
    }

    //请求数据的方法
    const GetGuestData = async () => 
    {
        let result = await  React.HTTP.post('/guest/GuestIndex', {
            busid: React.Business.id,
            page: page
        })

        if(result.code == 0)
        {
            //没有更多数据
            SethasMore(false)
            return false
        }
        else
        {
            //设置分页和列表合并
            SethasMore(true)
            SetPage(page + 1)
            SetList(list.concat(result.data))
        }
    }

    // 删除住客信息
    const GuestDelete = async (gid: number) =>
    {
        React.UI.Dialog.confirm({
            title: '删除',
            content: '是否确认删除住客信息?',
            onConfirm: async () => 
            {
                let result = await React.HTTP.post("/guest/GuestDel", {
                    busid: React.Business.id,
                    id: gid
                })

                if(result.code == 1)
                {
                    React.success(result.msg, () =>{
                        OnRefresh() //重新刷新列表
                    })
                }
                else
                {
                    React.error(result.msg)
                }
            }
        }).catch(() => {})
    }

    return (
        <>
            <div className={styles.list}>
                <React.UI.PullToRefresh onRefresh={OnRefresh}>
                    {/* 列表结构 */}
                    <React.UI.List>
                        {list.map((item: any, key: number) => (
                            <React.UI.SwipeAction
                                key={key}
                                rightActions={rightBtn}
                                onAction={GuestDelete.bind(this, item.id)}
                            >
                                <React.UI.List.Item
                                    key={key}
                                    description={item.mobile}
                                    onClick={() => React.navigate(`/guest/edit?gid=${item.id}`)}
                                >
                                    {item.nickname}({item.gender_text})
                                </React.UI.List.Item>
                            </React.UI.SwipeAction>
                        ))}
                    </React.UI.List>

                    {/* 上拉加载 */}
                    <React.UI.InfiniteScroll
                        loadMore={LoadMore}
                        hasMore={hasMore}
                        threshold={0}
                    />
                </React.UI.PullToRefresh>
            </div>

            <div className={styles.addguest}>
                <React.Router.Link to="/guest/add">添加住客</React.Router.Link>
            </div>
        </>
    );
};

export default GuestIndex;
